﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Web.Utility;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class PreviewOneQuestion
    {
        public string ConvertSecondToMinutes(int Seconds)
        {
            int Minute = Seconds / 60;
            if (Seconds % 6 != 0) Minute++;

            string ClassRemainTime = "cssTotalTime";
            if (Minute <= 5)
            {
                ClassRemainTime = "cssTotalTimeBlink";
            }

            return "<span class=" + ClassRemainTime + ">" + Minute.ToString("#,#00") + " phút</span>";

        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string QuestionId = (string)WebEnvironments.Request(SiteId, "QuestionId");
                OAjaxOut.HtmlContent=
                    "<div id=divFormExamContent>" + GetFormContent(SiteId, QuestionId) + "</div>";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static string GetFormContent(string SiteId,string QuestionId)
        {
            try
            {
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);
                if (QuestionCls == null)
                {
                    return "<h3>Không tìm thấy bài làm của thí sinh!</h3>";
                }


                string AnsText = "";
                if (QuestionCls.Type.ToLower().Equals("bestanswer"))
                {
                    DataTable dtItems = new QuestionBll().ReadingAnswer(SiteId, QuestionId);
                    AnsText = "<div class=cssHelpExam>Chọn 1 câu trả lời đúng cho các phương án dưới đây</div>" +
                              "<div id=divBestAnswerContent>" +
                              "<input type=hidden id=txtMaxItemAns value=\"" + dtItems.Rows.Count + "\">" +
                              "<ul class=cssUlAnswerText>";
                    for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                    {
                        string tAnswerTextFirst = (string)dtItems.Rows[iIndex]["AnswerTextFirst"];
                        string tCorrectAnswer = (string)dtItems.Rows[iIndex]["CorrectAnswer"];
                        if (tCorrectAnswer.Equals("YES"))
                        {
                            AnsText += "  <li><input readonly id=txtItemAns" + iIndex + " type=checkbox checked>&nbsp;" + tAnswerTextFirst + "</li>";
                        }
                        else
                        {
                            AnsText += "  <li><input readonly id=txtItemAns" + iIndex + " type=checkbox >&nbsp;" + tAnswerTextFirst + "</li>";
                        }
                    }
                    AnsText += "</ul>" +
                               "</div>";
                }
                if (QuestionCls.Type.ToLower().Equals("MultiChoice".ToLower()))
                {
                    DataTable dtItems = new QuestionBll().ReadingAnswer(SiteId, QuestionId);
                    AnsText = "<div class=cssHelpExam>Chọn các câu trả lời đúng cho các phương án dưới đây</div>" +
                              "<div id=divBestAnswerContent>" +
                              "<input type=hidden id=txtMaxItemAns value=\"" + dtItems.Rows.Count + "\">" +
                              "<ul class=cssUlAnswerText>";
                    for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                    {
                        string tAnswerTextFirst = (string)dtItems.Rows[iIndex]["AnswerTextFirst"];
                        string tCorrectAnswer = (string)dtItems.Rows[iIndex]["CorrectAnswer"];
                        if (tCorrectAnswer.Equals("YES"))
                        {
                            AnsText += "  <li><input readonly id=txtItemAns" + iIndex + " type=checkbox checked >&nbsp;" + tAnswerTextFirst + "</li>";
                        }
                        else
                        {
                            AnsText += "  <li><input readonly id=txtItemAns" + iIndex + " type=checkbox>&nbsp;" + tAnswerTextFirst + "</li>";
                        }
                    }
                    AnsText += "</ul>" +
                               "</div>";
                }


                if (QuestionCls.Type.ToLower().Equals("YesNo".ToLower()))
                {
                    AnsText += "<div>=>" + QuestionCls.CorrectOrder+ "</div>";
                }
                string[] allStatusId = new string[]{
                    "All","BestAnswer","MultiChoice","YesNo","FreeText"
                };

                string[] allStatusText = new string[]{
                    "== Toàn bộ==",
                    "Câu đúng nhất",
                    "Nhiều câu đúng",
                    "Đúng sai",
                    "Viết tự do",
                };


                string RetHtml =
                    //"<input type=button value='Quay lại' onclick=\"javascript:window.open('"+backUrl+"','_self');\">\r\n"+
                    "   <div id=divExamInfoContent>\r\n";

                if (QuestionCls.Question.Trim().Length == 0)
                {
                    RetHtml +=
                        "  <div class=cssExamQuestionTitle>" + QuestionCls.Title + " (" + QuestionCls.MaxPoint.ToString("#,##0") + " điểm)</div>\r\n";
                }
                else
                {
                    RetHtml +=
                        "       <div class=cssExamQuestionTitle>" +QuestionCls.Title+" ("+ QuestionCls.MaxPoint+" &nbsp;điểm)\r\n";

                    RetHtml += "</div>";
                    RetHtml+=
                        "<div style=\"text-align:left;\">" + QuestionCls.Question + "</div>";
                }



                if (AnsText.Trim().Length > 0)
                {
                    RetHtml +=
                        "       <div class=cssExamAnswerTitle>Các phương án trả lời</div>\r\n" +
                        "       <div class=cssExamAnswer>" + AnsText + "</div>\r\n";

                }
                
                RetHtml+=
                    "   </div>\r\n" +
                    "</div>\r\n";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }
    }
}
